package com.woniu02;

import java.awt.image.BufferedImageFilter;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) throws IOException {
        List<Student> students = new ArrayList<Student>();
        students.add(new Student(1,"zs",11,"male"));
        students.add(new Student(2,"ls",12,"female"));
        students.add(new Student(3,"ww",13,"male"));
        String myfilename = "student.txt";
        saveData(students,myfilename);
        System.out.println(loadData(myfilename));
    }


    public static List<Student> loadData(String fileName) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        List<Student> newstudents = new ArrayList<Student>();
        String s ;
        while ((s=br.readLine()) != null) {
            String[] result = s.split("-");
            newstudents.add(new Student(Integer.parseInt(result[0]),result[1],Integer.parseInt(result[2]),result[3]));
        }
        br.close();
        return newstudents;
    }

    public static void saveData(List<Student> students,String myfilename) throws IOException {
        BufferedWriter bos = new BufferedWriter(new FileWriter(myfilename));

        for (int i = 0; i < students.size(); i++) {
            String s = students.get(i).getId() + "-" + students.get(i).getName() + "-" + students.get(i).getAge() +"-"+ students.get(i).getGender();
            bos.write(s);
            bos.newLine();
            bos.flush();
        }
        bos.close();
    }


}
